/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.pay.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bblocks.util.StringUtil;
import com.pig4cloud.pigx.pay.entity.PayTradeOrder;
import com.pig4cloud.pigx.pay.mapper.PayTradeOrderMapper;
import com.pig4cloud.pigx.pay.service.PayTradeOrderService;
import lombok.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付
 *
 * @author lengleng
 * @date 2019-05-28 23:58:18
 */
@Service
public class PayTradeOrderServiceImpl extends ServiceImpl<PayTradeOrderMapper, PayTradeOrder>
		implements PayTradeOrderService {

	public PayTradeOrder saveOne(PayTradeOrder tradeOrder){
		tradeOrder.setPayNo(StringUtil.getOrderNum(tradeOrder.getUserId(),9,2));
		if(StrUtil.isEmpty(tradeOrder.getCurrency()))
			tradeOrder.setCurrency("cny");

		if(tradeOrder.getExpireTime() == null)
			tradeOrder.setExpireTime(30L);

		if(tradeOrder.getPayType() == null)
			tradeOrder.setPayType(1);

		return tradeOrder;
	}

	public PayTradeOrder getByTendIdAndReqNo(Long tenantId ,@NonNull String reqNo){
		LambdaQueryWrapper<PayTradeOrder> queryWrapper = new LambdaQueryWrapper();
		if(tenantId != null)
			queryWrapper.eq(PayTradeOrder::getTenantId,tenantId);
		else
			queryWrapper.isNotNull(PayTradeOrder::getTenantId);
		queryWrapper.eq(PayTradeOrder::getReqNo,reqNo);
		queryWrapper.eq(PayTradeOrder::getDelFlag,"0");

		return this.baseMapper.selectOne(queryWrapper);
	}
}
